package com.lsh.oms.provider.service.es;

import com.alibaba.dubbo.config.annotation.Service;
import com.alibaba.dubbo.rpc.protocol.rest.support.ContentType;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lsh.base.common.model.CommonResult;
import com.lsh.oms.api.service.es.EsService;
import com.lsh.oms.core.exception.BusinessException;
import com.lsh.oms.core.exception.ExceptionStatus;
import com.lsh.oms.core.model.order.OrderShippingHeadWithBLOBs;
import com.lsh.oms.core.model.order.OrderSignHead;
import com.lsh.oms.core.service.order.OrderDeliveryService;
import com.lsh.oms.core.service.order.OrderQueryService;
import com.lsh.oms.core.service.other.ShippingOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

/**
 * Project Name: lsh-oms
 *
 * @author peter
 * @date 2020-03-31
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
@Service(protocol = "rest", timeout = 30000)
@Path("order/es")
@Consumes({MediaType.APPLICATION_JSON})
@Produces({ContentType.APPLICATION_JSON_UTF_8})
@Slf4j
public class EsServiceImpl implements EsService {

    @Autowired
    private OrderQueryService orderQueryService;

    @Autowired
    private OrderDeliveryService orderDeliveryService;

    @Autowired
    private ShippingOrderService shippingService;

    /**
     * create
     *
     * @param type 参数
     * @return CommonResult
     */
    @GET
    @Path("addall")
    @Override
    public CommonResult<Object> dealEs(String type) {

        CommonResult<Object> commonResult = new CommonResult<>();
        try {
            this.esSh();

//            log.info("订单dealEs fetchData start...");
//            List<OrderHead> list = this.orderQueryService.findListByStatusRange(22, 96, 1577808000, 1582560000);
//            log.info("订单dealEs fetchData size=" + list.size());
//
//            for (OrderHead orderHead : list) {
//                orderDeliveryService.dealEs(orderHead);
//
//                notifyEsOrderInfo(orderHead.getOrderCode(), OrderFactory.getMarketName(orderHead), OrderFactory.getProviderId(orderHead), "");
//            }

            log.info("commonResult-" + JSON.toJSONString(commonResult));
        } catch (BusinessException e) {
            commonResult.setCode(e.getCode());
            commonResult.setMessage(e.getMessage());
            log.error("业务异常code : " + e.getCode() + ",异常信息: " + e.getMessage(), e);
        } catch (Exception e) {
            commonResult.setCode(ExceptionStatus.E3001001.getCode());
            commonResult.setMessage(ExceptionStatus.E3001001.getMessage());
            log.error("业务异常code : " + ExceptionStatus.E3001001.getCode(), e);
        }

        return commonResult;
    }

    private boolean esSh() {
        try {
            String path = this.getClass().getClassLoader().getResource("props/order_mapping.sh").getPath();
            log.info("es path is " + path);
            Runtime.getRuntime().exec("sh " + path);

            path = this.getClass().getClassLoader().getResource("props/shipping_mapping.sh").getPath();
            log.info("es path is " + path);
            Runtime.getRuntime().exec("sh " + path);

            path = this.getClass().getClassLoader().getResource("props/receipt_mapping.sh").getPath();
            log.info("es path is " + path);
            Runtime.getRuntime().exec("sh " + path);
            return true;
        } catch (Exception e) {
            log.error("es index 异常", e);
            return false;
        }

    }

    private void notifyEsOrderInfo(Long orderCode, String marketName, String providerId, String logSign) {
        try {
            OrderShippingHeadWithBLOBs shippingEs = shippingService.getBolbs(orderCode);

            if (null == shippingEs) {
                return;
            }

            shippingEs.setMarketName(marketName);
            shippingEs.setProviderId(providerId);
            String addressInfo = shippingEs.getAddressInfo();
            if (StringUtils.isNotBlank(addressInfo)) {
                JSONObject address = JSON.parseObject(addressInfo);
                shippingEs.setAddressTelephone(address.getString("contact_phone"));
            }
            orderDeliveryService.dealsEs(shippingEs);
            OrderSignHead orderSignHead = shippingService.selectAll(orderCode);

            if (null == orderSignHead) {
                return;
            }
            orderSignHead.setMarketName(marketName);
            orderSignHead.setProviderId(providerId);
            orderSignHead.setHgReceiptStatus(0);
            orderSignHead.setFinalMoney(orderSignHead.getMoney());
            orderDeliveryService.dealrEs(orderSignHead);

        } catch (Exception e) {
            log.info(logSign + "订单号" + orderCode + ",生成发货单同步es失败。");
            log.info(e.getMessage(), e);
        }
    }

    private void esMapping() {


    }


}
